import os
from PIL import Image
import imagehash

def get_image_hash(folder_path):
    image_hashes = {}
    valid_extensions = ('.jpg', '.jpeg', '.png', '.bmp', '.gif', '.tiff', '.webp')

    for filename in os.listdir(folder_path):
        if filename.lower().endswith(valid_extensions):
            file_path = os.path.join(folder_path, filename)
            try:
                with Image.open(file_path) as img:
                    # 计算感知哈希 (pHash)
                    img_hash = imagehash.phash(img)
                    image_hashes[filename] = str(img_hash)
            except Exception as e:
                print(f"处理图片 {filename} 时出错: {str(e)}")

    return image_hashes

if __name__ == '__main__':
    path = r"C:\Users\15328\OneDrive\图片\BiteLive\screenshot"
    hashs = get_image_hash(path)
    arr = ["a2f24cd176f04cf2", "a2f24cd076f06cf2"]
    for f in range(len(arr)):
        for filename, hash in hashs.items():
            if arr[f] == str(hash):
                print("相同")
                print(f"{filename} : {hash}")
                print(f)
                print("\n")



